﻿using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.Auditing;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.Json;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Zero.Configuration;
using AbpProjectTemplate.Application;
using AbpProjectTemplate.EntityFrameworkCore;
using AbpProjectTemplate.Web.Auditing;
using AbpProjectTemplate.Web.Authentication.External;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using AbpProjectTemplate.Web.Configuration;
using AbpProjectTemplate.Localization;
using Abp.Web;
using Abp.Zero;

namespace AbpProjectTemplate.Web
{
    [DependsOn(
         typeof(AppApplicationModule),
         typeof(AppEntityFrameworkModule),
         typeof(AbpAspNetCoreModule)
     )]
    public class AppWebCoreModule : AbpModule
    {
        private readonly IWebHostEnvironment _env;
        private readonly IConfigurationRoot _appConfiguration;

        public static readonly string BaseNamespace = typeof(AppWebCoreModule).Namespace;

        public AppWebCoreModule(IWebHostEnvironment env)
        {
            this._env = env;
            this._appConfiguration = env.GetAppConfiguration();
        }

        public override void PreInitialize()
        {
            this.Configuration.DefaultNameOrConnectionString = this._appConfiguration.GetConnectionString(
                AppConsts.ConnectionStringName
            );
            this.Configuration
                .ReplaceService<IClientInfoProvider, AppHttpContextClientInfoProvider>(DependencyLifeStyle.Transient);
            this.Configuration.Localization.ExtendXmlSource(this, AbpWebConsts.LocalizationSourceName);

        }

        private void ConfigureExternalAuth()
        {
            var externalAuthConfiguration = this.IocManager.Resolve<IExternalAuthConfiguration>();

            //var config = AppSettings.Instance;

            //this.Logger.Debug(config.ToJsonString());
            //var wechantLoginConfig = IocManager.Resolve<IOptions<WechatMiniProgramConfig>>().Value;
            ////小程序登录
            //if (wechantLoginConfig.IsEnabled)
            //{
            //    externalAuthConfiguration.Providers.Add(new ExternalLoginProviderInfo(
            //        WechatMiniProgramProviderApi.ProviderName,
            //        wechantLoginConfig.AppId,
            //        wechantLoginConfig.AppSecret,
            //        typeof(WechatMiniProgramProviderApi)));

            //    AsyncHelper.RunSync(() => AccessTokenContainer.RegisterAsync(wechantLoginConfig.AppId, wechantLoginConfig.AppSecret));
            //}
            ////AccessTokenContainer.RegisterAsync(wechantLoginConfig.AppId, wechantLoginConfig.AppSecret, "DL");
            //var alipayLoginConfig = IocManager.Resolve<IOptions<AlipayMiniProgramConfig>>().Value;
            ////小程序登录
            //if (alipayLoginConfig.IsEnabled)
            //{
            //    externalAuthConfiguration.Providers.Add(new ExternalLoginProviderInfo(
            //        AlipayMiniProgramProviderApi.ProviderName,
            //        alipayLoginConfig.AppId,
            //        alipayLoginConfig.AppSecret,
            //        typeof(AlipayMiniProgramProviderApi)));
            //}
        }

        public override void Initialize()
        {
            this.IocManager.RegisterAssemblyByConvention(typeof(AppWebCoreModule).GetAssembly());
        }

        public override void PostInitialize()
        {
            base.PostInitialize();
            this.ConfigureExternalAuth();
        }
    }
}

